home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GP_IWATC.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  4KB  |  154 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_iwatc.c */
  20. /* Intel processor, Watcom C-specific routines for Ghostscript */
  21. #include "dos_.h"
  22. #include <fcntl.h>
  23. #include <signal.h>
  24. #include <stdlib.h>
  25. #include "stat_.h"
  26. #include "string_.h"
  27. #include "gx.h"
  28. #include "gp.h"
  29.  
  30. /* Library routines not declared in a standard header */
  31. extern char *getenv(P1(const char *));
  32.  
  33. /* Define a substitute for stdprn (see below). */
  34. private FILE *gs_stdprn;
  35.  
  36. /* Forward declarations */
  37. private void handle_FPE(P1(int));
  38.  
  39. /* Do platform-dependent initialization. */
  40. void
  41. gp_init(void)
  42. {    gs_stdprn = 0;
  43.     /* Set up the handler for numeric exceptions. */
  44.     signal(SIGFPE, handle_FPE);
  45.     gp_init_console();
  46. }
  47.  
  48. /* Trap numeric exceptions.  Someday we will do something */
  49. /* more appropriate with these. */
  50. private void
  51. handle_FPE(int sig)
  52. {    eprintf("Numeric exception:\n");
  53.     exit(1);
  54. }
  55.  
  56. /* Do platform-dependent cleanup. */
  57. void
  58. gp_exit(int exit_status, int code)
  59. {
  60. }
  61.  
  62. /* ------ Printer accessing ------ */
  63.  
  64. /* Open a connection to a printer.  A null file name means use the */
  65. /* standard printer connected to the machine, if any. */
  66. /* Return NULL if the connection could not be opened. */
  67. extern void gp_set_printer_binary(P2(int, int));
  68. FILE *
  69. gp_open_printer(char *fname, int binary_mode)
  70. {    if ( strlen(fname) == 0 || !strcmp(fname, "PRN") )
  71.     {    if ( !binary_mode )
  72.             return stdprn;
  73.         if ( gs_stdprn == 0 )
  74.            {    /* We have to effectively reopen the printer, */
  75.             /* because the Watcom library does \n -> \r\n */
  76.             /* substitution on the stdprn stream. */
  77.             int fno = dup(fileno(stdprn));
  78.             setmode(fno, O_BINARY);
  79.             gs_stdprn = fdopen(fno, "wb");
  80.             gp_set_printer_binary(fileno(gs_stdprn), 1);
  81.            }
  82.         return gs_stdprn;
  83.     }
  84.     else
  85.         return fopen(fname, (binary_mode ? "wb" : "w"));
  86. }
  87.  
  88. /* Close the connection to the printer. */
  89. void
  90. gp_close_printer(FILE *pfile, const char *fname)
  91. {    if ( pfile != stdprn )
  92.         fclose(pfile);
  93.     if ( pfile == gs_stdprn )
  94.         gs_stdprn = 0;
  95. }
  96.  
  97. /* ------ File names ------ */
  98.  
  99. /* Create and open a scratch file with a given name prefix. */
  100. /* Write the actual file name at fname. */
  101. FILE *
  102. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  103. {    /* Unfortunately, Watcom C doesn't provide mktemp, */
  104.     /* so we have to simulate it ourselves. */
  105.     char *temp;
  106.     struct stat fst;
  107.     char *end;
  108.     if ( (temp = getenv("TEMP")) == NULL )
  109.         *fname = 0;
  110.     else
  111.     {    char last = '\\';
  112.         strcpy(fname, temp);
  113.         /* Prevent X's in path from being converted by mktemp. */
  114.         for ( temp = fname; *temp; temp++ )
  115.             *temp = last = tolower(*temp);
  116.         switch ( last )
  117.         {
  118.         default:
  119.             strcat(fname, "\\");
  120.         case ':': case '\\':
  121.             ;
  122.         }
  123.     }
  124.     strcat(fname, prefix);
  125.     strcat(fname, "AA.AAA");
  126.     end = fname + strlen(fname) - 1;
  127.     while ( stat(fname, &fst) == 0 )
  128.        {    char *inc = end;
  129.         while ( *inc == 'Z' || *inc == '.' )
  130.            {    if ( *inc == 'Z' ) *inc = 'A';
  131.             inc--;
  132.             if ( end - inc == 6 ) return 0;
  133.            }
  134.         ++*inc;
  135.        }
  136.     return fopen(fname, mode);
  137. }
  138.  
  139. /* ------ File operations ------ */
  140.  
  141. /* If the file given by fname exists, fill in its status and return 1; */
  142. /* otherwise return 0. */
  143. int
  144. gp_file_status(const char *fname, file_status *pstatus)
  145. {    struct stat fst;
  146.     if ( stat(fname, &fst) < 0 ) return 0;
  147.     pstatus->size_pages = (fst.st_size + 1023) >> 10;
  148.     pstatus->size_bytes = fst.st_size;
  149.     /****** CONVERSION PROBABLY REQUIRED HERE ******/
  150.     pstatus->time_referenced = fst.st_atime;
  151.     pstatus->time_created = fst.st_mtime;
  152.     return 1;
  153. }
  154.